package hell.p2p.base;

import java.io.IOException;
import java.net.*;
import java.util.Queue;
import java.util.concurrent.LinkedTransferQueue;


public class UDPServer {
    private DatagramSocket socket;
    private String name;
    private Status status;
    private Thread local;
    private Thread sender;
    private Queue<Messgram> send_queue;
    private boolean  active;

    public UDPServer(int port, String name, String address)
    {
        try {
            socket = new DatagramSocket(port, InetAddress.getByName(address));
        } catch (SocketException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (UnknownHostException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        this.name = name;
        status=Status.worker;
        active=true;
        send_queue = new LinkedTransferQueue<Messgram>();



        local = new Thread(new Runnable() {
            @Override
            public void run() {

                do{
                    DatagramPacket tmp=new DatagramPacket(new byte[30],30);
                    try {

                        socket.receive(tmp);
                        parsePacket(tmp);



                    } catch (IOException e) {
                      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }

                } while (active);

            }
        });
        sender = new Thread(new Runnable() {
            @Override
            public void run() {
                do{
                    DatagramPacket tmp;

                        if(!send_queue.isEmpty()){

                            Messgram ms = send_queue.poll();
                            tmp = new DatagramPacket(ms.getData(),ms.getData().length,ms.getAddress(),667);
                            System.out.println(tmp.getAddress().toString());
                            try {
                                System.out.println("send");
                                socket.send(tmp);




                            } catch (IOException e) {
                                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                            }

                        }





                } while (active);

            }
        });
        local.start();
        sender.start();





    }

    public void Send(Messgram ms){

        synchronized (send_queue){
            send_queue.add(ms);
            System.out.println("add");
        }

    }
    public void parsePacket(DatagramPacket data){

        try{





            byte [] info = new byte[data.getData().length];
            info = data.getData();

            if(info.length>=3){
                System.out.println("smth recieved");
                switch (info[0]){

                    case 0:{
                        switch (info[1]){
                            case 0:{break;}
                            case 1:{break;}
                            case 2:{break;}
                            case 3:{break;}
                            case 4:{break;}
                            case 5:{break;}
                            case 6:{break;}
                            case 7:{break;}
                            case 8:{break;}
                        }
                        break;
                    }
                    case 1:{

                        switch (info[1]){
                            case 0:{break;}
                            case 1:{break;}
                            case 2:{break;}
                            case 3:{break;}

                        }
                        break;
                    }
                    case 2:{

                        switch (info[1]){
                            case 0:{break;}
                            case 1:{break;}
                            case 2:{break;}
                            case 3:{break;}

                        }
                        break;
                    }

                    case 8:{
                        System.out.println("aaaaaaaa");

                        this.stop();
                        break;
                    }






                }






            }
           System.out.println(data.getAddress().getHostAddress());
        } catch (NullPointerException e){
            e.printStackTrace();
        }


    }

    public void stop() {

        active=false;

        local.stop();
        sender.stop();

    }

    public InetAddress getAddress(){

        return socket.getInetAddress();
    }

    public Status getStatus(){

        return status;
    }

    public boolean isActive() {
        return active;
    }
}

